<template>
  <div class="index">
    <!-- 头部带logo的导航 -->
    <div>
      <van-nav-bar fixed>
        <template v-slot:left>
          <img class="logo" src="@/assets/logo_syf.png" alt="" />
        </template>
        <template #right>
          <van-icon
            name="search"
            size="0.48rem"
            color="#fff"
            @click="moveSearchPageFn"
          />
        </template>
      </van-nav-bar>
    </div>

    <!-- 第二层导航 -->
    <div class="main">
      <van-tabs
        v-model="channelID"
        animated
        sticky
        offset-top="1.226667rem"
        @change="channelChangeFn"
      >
        <van-tab
          :title="item.name"
          v-for="item in userChannelList"
          :name="item.id"
          :key="item.id"
        >
          <!-- 细节
        van-tab循环了很多标签导航，与之一一对应的内容列表不是上来都创建的，默认
        创建当前激活导航对应列表组件
        第一次切换到对应频道时，才会创建下属的ArticleList组件 -> created方法
        第二次切换就是显示/隐藏切换
         -->
          <ArticleList :channelID="channelID"></ArticleList>
        </van-tab>
      </van-tabs>

      <!-- 编辑频道图标 -->
      <van-icon
        name="plus"
        size="0.37333334rem"
        class="moreChannels"
        @click="show = true"
      />
    </div>

    <!-- 频道管理弹出层 -->
    <van-popup v-model="show" class="channelPopup">
      <channelEdit
        :userChannelList="userChannelList"
        :unCheckChannelList="unCheckChannelList"
        @addChannelEV="addChannelFn"
        @removeChannelEV="removeChannelFn"
        @closeEV="show = false"
        v-model="channelID"
      ></channelEdit>
    </van-popup>
  </div>
</template>

<script>
import {
  getUserChannelsAPI,
  getAllChannelsAPI,
  updateUserChannelsAPI,
  removeChannelAPI,
} from "@/api";
import ArticleList from "./components/ArticleList.vue";
import channelEdit from "./channelEdit.vue";
export default {
  name: "index",
  data() {
    return {
      channelID: 0,
      userChannelList: [], //用户已选列表
      allChannelList: [], //全部频道列表

      articleList: [], //文章列表
      show: false, //控制频道管理弹出层
      channelScrollTObj: {}, //保存每个频道的滚动位置
      //值构想：{推荐评到ID：滚动距离，html频道ID：滚动距离}
    };
  },
  async created() {
    const res = await getUserChannelsAPI();
    this.userChannelList = res.data.data.channels;

    const res2 = await getAllChannelsAPI();
    this.allChannelList = res2.data.data.channels;
  },
  methods: {
    async addChannelFn(item) {
      this.userChannelList.push(item);

      const newArr = this.userChannelList.filter((item) => {
        return item.id !== 0;
      });

      const newChannelsArr = newArr.map((item, index) => {
        const newObj = { ...item };
        delete newObj.name;
        newObj.seq = index;

        return newObj;
      });

      const res = await updateUserChannelsAPI({
        channels: newChannelsArr,
      });
    },
    async removeChannelFn(item) {
      await removeChannelAPI({ channelId: item.id });

      const res = await getUserChannelsAPI();
      this.userChannelList = res.data.data.channels;
    },
    moveSearchPageFn() {
      this.$router.push("/search");
    },
    channelChangeFn() {
      //tab切换时，组件内部，会把窃走的容器height设置为0，滚动条因为没有高度回到了顶部
      //切回来一瞬间，没有高度，滚动事件1从底下上来也被触发了，所以才把数据里设置为0
      //切回来的一瞬间，高度为0，你设置滚动位置也无用
      this.$nextTick(() => {
        document.documentElement.scrollTop =
          this.channelScrollTObj[this.channelID];

        document.body.scrollTop = this.channelScrollTObj[this.channelID];
      });
    },
    scrollFn() {
      this.$route.meta.scrollT =
        document.documentElement.scrollTop || document.body.scrollTop; //谷歌滚动条是document.documentElement,安卓是document.body

      //同时保存当前频道的滚动距离
      this.channelScrollTObj[this.channelID] =
        document.documentElement.scrollTop || document.body.scrollTop;
    },
  },
  computed: {
    unCheckChannelList() {
      const newArr = this.allChannelList.filter((item) => {
        const index = this.userChannelList.findIndex((ite) => {
          return ite.id === item.id;
        });
        //找到了
        if (index > -1) {
          return false;
        } else {
          return true;
        }
      });
      return newArr;
    },
  },
  components: {
    ArticleList,
    channelEdit,
  },
  activated() {
    window.addEventListener("scroll", this.scrollFn);
    //window和document,监听网页滚动的事件
    //html标签获取scrollTop,滚动的距离，和设置滚动的位置
    //激活组件立即设置，滚动条位置
    document.documentElement.scrollTop = this.$route.meta.scrollT;

    document.body.scrollTop = this.$route.meta.scrollT;
  },
  deactivated() {
    window.removeEventListener("scroll", this.scrollFn);
  },
};
</script>

<style lang="less" scoped>
.logo {
  width: 100px;
  height: 30px;
}
.main {
  padding-top: 46px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 63px;
  right: 8px;
  z-index: 999;
}
.channelPopup {
  width: 100vw;
  height: 100vh;
}
</style>
